package com.example.a79273.shandianban.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.a79273.shandianban.R;
import com.example.a79273.shandianban.config.NetConfigYouRen;
import com.example.a79273.shandianban.model.YouRenPageBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 有人页面下面的Viewpager下的fragment的适配器
 */
public class YouRenRecyclerViewAdapter extends RecyclerView.Adapter<YouRenRecyclerViewAdapter.MyViewHolder> {

    private List<YouRenPageBean.DataBean.ListBean> mData;
    private Context mContext;
    private LayoutInflater inflater;
    private Picasso picasso;


    public YouRenRecyclerViewAdapter(List<YouRenPageBean.DataBean.ListBean> data, Context context) {
        mData = data;
        mContext = context;
        inflater = LayoutInflater.from(context);
        picasso = Picasso.with(context);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.item_youren, null);

        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.image02_rl.setVisibility(View.VISIBLE);
        holder.image03_rl.setVisibility(View.VISIBLE);
        holder.image04_rl.setVisibility(View.VISIBLE);
        holder.image05_rl.setVisibility(View.VISIBLE);
        int count = Math.min((position + 1) * 5, mData.size());

        int location = 0;
        for (int i = position * 5; i < count; i++) {
            YouRenPageBean.DataBean.ListBean bean = mData.get(i);
            picasso.load(NetConfigYouRen.ROOT_URL + bean.getAsrc()).into(holder.images[location]);
            holder.fansCount[location].setText("" + bean.getFans());
            holder.designers[location].setText(bean.getName());
            location++;
        }

        for (int j = location; j < 5; j++) {
            holder.imagesContainer[j].setVisibility(View.GONE);
        }

    }


    @Override
    public int getItemCount() {
        int itemCount = 0;
        int size = mData.size();
        int count = size / 5;

        if (size % 5 == 0) {
            itemCount = count;
        } else {
            itemCount = count + 1;
        }
        return mData == null ? 0 : itemCount;
    }


    public static class MyViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout image01_rl, image02_rl, image03_rl, image04_rl, image05_rl;
        private ImageView image01_iv, image02_iv, image03_iv, image04_iv, image05_iv;
        private TextView fansCount01_tv, designer01_tv,
                fansCount02_tv, designer02_tv,
                fansCount03_tv, designer03_tv,
                fansCount04_tv, designer04_tv,
                fansCount05_tv, designer05_tv;
        private RelativeLayout[] imagesContainer;
        private ImageView[] images;
        private TextView[] fansCount, designers;

        public MyViewHolder(View itemView) {
            super(itemView);
            image01_rl = (RelativeLayout) itemView.findViewById(R.id.rl_item_youren_image01);
            image01_iv = (ImageView) image01_rl.findViewById(R.id.iv_item_item_youren_image);
            fansCount01_tv = (TextView) image01_rl.findViewById(R.id.iv_item_item_youren_fansCount);
            designer01_tv = (TextView) image01_rl.findViewById(R.id.tv_item_item_youren_designer);

            image02_rl = (RelativeLayout) itemView.findViewById(R.id.rl_item_youren_image02);
            image02_iv = (ImageView) image02_rl.findViewById(R.id.iv_item_item_youren_image);
            fansCount02_tv = (TextView) image02_rl.findViewById(R.id.iv_item_item_youren_fansCount);
            designer02_tv = (TextView) image02_rl.findViewById(R.id.tv_item_item_youren_designer);

            image03_rl = (RelativeLayout) itemView.findViewById(R.id.rl_item_youren_image03);
            image03_iv = (ImageView) image03_rl.findViewById(R.id.iv_item_item_youren_image);
            fansCount03_tv = (TextView) image03_rl.findViewById(R.id.iv_item_item_youren_fansCount);
            designer03_tv = (TextView) image03_rl.findViewById(R.id.tv_item_item_youren_designer);

            image04_rl = (RelativeLayout) itemView.findViewById(R.id.rl_item_youren_image04);
            image04_iv = (ImageView) image04_rl.findViewById(R.id.iv_item_item_youren_image);
            fansCount04_tv = (TextView) image04_rl.findViewById(R.id.iv_item_item_youren_fansCount);
            designer04_tv = (TextView) image04_rl.findViewById(R.id.tv_item_item_youren_designer);

            image05_rl = (RelativeLayout) itemView.findViewById(R.id.rl_item_youren_image05);
            image05_iv = (ImageView) image05_rl.findViewById(R.id.iv_item_item_youren_image);
            fansCount05_tv = (TextView) image05_rl.findViewById(R.id.iv_item_item_youren_fansCount);
            designer05_tv = (TextView) image05_rl.findViewById(R.id.tv_item_item_youren_designer);


            imagesContainer = new RelativeLayout[]{image01_rl, image02_rl, image03_rl, image04_rl, image05_rl};
            images = new ImageView[]{image01_iv, image02_iv, image03_iv, image04_iv, image05_iv};
            fansCount = new TextView[]{fansCount01_tv, fansCount02_tv, fansCount03_tv, fansCount04_tv, fansCount05_tv};
            designers = new TextView[]{designer01_tv, designer02_tv, designer03_tv, designer04_tv, designer05_tv};
        }
    }


}
